<!--
  - Copyright 2014-2018 the original author or authors.
  -
  - Licensed under the Apache License, Version 2.0 (the "License");
  - you may not use this file except in compliance with the License.
  - You may obtain a copy of the License at
  -
  -     http://www.apache.org/licenses/LICENSE-2.0
  -
  - Unless required by applicable law or agreed to in writing, software
  - distributed under the License is distributed on an "AS IS" BASIS,
  - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  - See the License for the specific language governing permissions and
  - limitations under the License.
  -->

<template>
  <table class="table is-hoverable is-fullwidth">
    <thead>
      <tr>
        <th>Timestamp</th>
        <th>Event</th>
        <th>Principal</th>
        <th>Remote address</th>
        <th>Session Id</th>
      </tr>
    </thead>
    <tbody>
      <template v-for="event in events">
        <tr class="is-selectable"
            :class="{ 'event--is-detailed' : showDetails[event.key] }"
            @click="showDetails[event.key] ? $delete(showDetails, event.key) : $set(showDetails, event.key, true)"
            :key="event.key">
          <td v-text="event.timestamp.format('L HH:mm:ss.SSS')"/>
          <td>
            <span v-text="event.type" class="tag"
                  :class="{ 'is-success' : event.isSuccess(), 'is-danger' : event.isFailure() }"/>
          </td>
          <td v-if="hasSessionEndpoint && event.principal">
            <router-link v-text="event.principal"
                         :to="{ name: 'instance/sessions', params: { 'instanceId' : instance.id }, query: { username : event.principal} }"/>
          </td>
          <td v-else v-text="event.principal"/>
          <td v-text="event.remoteAddress"/>
          <td v-if="hasSessionEndpoint && event.sessionId">
            <router-link v-text="event.sessionId"
                         :to="{ name: 'instance/sessions', params: { 'instanceId' : instance.id }, query: { sessionId : event.sessionId } }"/>
          </td>
          <td v-else v-text="event.sessionId"/>
        </tr>
        <tr class="event__detail" :key="`${event.key}-detail`" v-if="showDetails[event.key]">
          <td colspan="5">
            <pre v-text="toJson(event.data)"/>
          </td>
        </tr>
      </template>
      <tr v-if="events.length === 0">
        <td class="is-muted" colspan="5">No auditevents found.</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
  import Instance from '@/services/instance';
  import prettyBytes from 'pretty-bytes';

  export default {
    props: {
      events: {
        type: Array,
        default: () => []
      },
      instance: {
        type: Instance,
        required: true
      }
    },
    data: () => ({
      showDetails: {}
    }),
    computed: {
      hasSessionEndpoint() {
        return this.instance.hasEndpoint('sessions');
      }
    },
    methods: {
      prettyBytes,
      toJson(obj) {
        return JSON.stringify(obj, null, 4);
      }
    }
  }
</script>

<style lang="scss">
  @import "~@/assets/css/utilities";

  .event--is-detailed td {
    border: none !important;
  }

  .event__detail td {
    overflow-x: auto;
    max-width: 1024px;
  }
</style>
